(function ($) {
    "use strict"; // jshint
    var Helper = function (element, options) {
            this.options = $.extend({}, $.fn.helper.defaults, options);
            this.$element = $(element);
            this.$group = this.$element.parents(".control-group");

            this.$helper = this.$group.find("help-block");
            if (this.$helper.length === 0) {

                this.$helper = $('<span class="help-block"></span>');
                this.$group.find(".controls").append(this.$helper);
            }
        };

    Helper.prototype.state = function (option) {
        this.$group.removeClass("error warning success").addClass(option.type);
        this.$helper.text(option.text);
        if (option.type !== "error" && option.text === "" ) {
            this.$group.removeClass("error success");
        }
    };


    /* Helper Plugin Definition
    ================================== */
    $.fn.helper = function (type, text) {
        return this.each(function () {
            var $self = $(this),
                data = $self.data('helper'),
                option = {type: type, text: text};
            if (!data) {
                $self.data('helper', (data = new Helper($self, option)));
            }
            data.state(option);
        });
    };

    $.fn.helper.defaults = {
        text:  '',
        type:  "error"
    };

    $.fn.helper.Constructor = Helper;


}(window.jQuery));
